import 'package:flutter_shop_z/config/service_url.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop_z/pages/details_page.dart';

class Recommend extends StatelessWidget {
  final List recommendList; //推荐商品列表
  Recommend(this.recommendList);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenUtil().setHeight(400),
      margin: EdgeInsets.only(top: 10),
      child: Column(
        children: <Widget>[_titleWidget(), _recommendList()],
      ),
    );
  }

//推荐商品标题
  Widget _titleWidget() {
    return Container(
      alignment: Alignment.centerLeft, //上下居中 靠左
      padding: EdgeInsets.fromLTRB(10.0, 2.0, 0, 5.0),
      decoration: BoxDecoration(
          color: Colors.white,
          border:
              Border(bottom: BorderSide(width: 0.5, color: Colors.black12))),
      child: Text(
        '商品推荐',
        style: TextStyle(color: Colors.purple),
      ),
    );
  }

  //推荐商品
  Widget _recommendList() {
    return Container(
      height: ScreenUtil().setHeight(330),
      child: ListView.builder(
        scrollDirection: Axis.horizontal, //左右滑动
        itemCount: recommendList.length,
        itemBuilder: (context, index) {
          return _item(context, index);
        },
      ),
    );
  }

  Widget _item(context, index) {
    return InkWell(
      onTap: () {
        Navigator.push(context, MaterialPageRoute(builder: (context) {
          return DetailsPage(recommendList[index]['commodityId']);
        }));
      },
      child: Container(
        height: ScreenUtil().setHeight(330),
        width: ScreenUtil().setWidth(250),
        padding: EdgeInsets.all(8),
        decoration: BoxDecoration(
            color: Colors.white,
            border:
                Border(left: BorderSide(width: 0.5, color: Colors.black12))),
        child: Column(
          children: <Widget>[
            Image.network(
              "${serviceUrl}goods/${recommendList[index]['categoryId']}/${recommendList[index]['categorySubId']}/${recommendList[index]['commodityImg']}",
              fit: BoxFit.fitWidth,
              height: ScreenUtil().setHeight(220),
            ),
            Text("￥${recommendList[index]['commodityDiscountPrice']}"),
            Text(
              "￥${recommendList[index]['commodityOriginalPrice']}",
              style: TextStyle(
                  decoration: TextDecoration.lineThrough, //中划线
                  color: Colors.grey),
            )
          ],
        ),
      ),
    );
  }
}
